package simulateOS.UI;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import simulateOS.entity.Disk;
import simulateOS.entity.DiskData;
import simulateOS.entity.FAT;
import simulateOS.entity.Folder;

import java.io.File;


public class DiskUI {
    private BorderPane Pane = new BorderPane();
    private PieChart pieChart = new PieChart();
    private GridPane gridPane = new GridPane();
    private Scene scene;
    private static Stage stage = new Stage();
    private static DiskUI diskUI = null;
    private PieChart.Data p1 = null;
    private PieChart.Data p2 = null;
    private ObservableList<DiskData> diskData = FXCollections.observableArrayList();
    private TableView<DiskData> diskDataTableView = new TableView<>();

    public static DiskUI getDiskUI(){
        if(diskUI == null){
            return new DiskUI();
        }else {
            return diskUI;
        }
    }

    private DiskUI(){
        initTheGridPane();
        VBox vBox = new VBox();
        vBox.getChildren().addAll(gridPane,pieChart);
        Pane.setCenter(vBox);
        Pane.setRight(diskDataTableView);
        scene = new Scene(Pane,850,650);
        stage.setScene(scene);
        stage.setTitle("磁盘");
//        File imagePath = new File("src\\main\\resources\\image");/*图片路径*/
        stage.getIcons().add(new Image(DiskUI.class.getClassLoader().getResourceAsStream("image/MyDisk.png")));
    }

    public Stage getStage() {
        return stage;
    }

    public void initTheGridPane(){
        int w = 15,h = 15;
        Label[] tips = new Label[16];
        for(int i = 0;i < 16;i++){
            String word = new String();
            switch (i){
                case 0:{
                    word = "盘块0~~~15";
                    break;
                }
                case 1:{
                    word = "盘块16~~~31";
                    break;
                }
                case 2:{
                    word = "盘块32~~~47";
                    break;
                }
                case 3:{
                    word = "盘块48~~~63";
                    break;
                }
                case 4:{
                    word = "盘块64~~~79";
                    break;
                }
                case 5:{
                    word = "盘块80~~~95";
                    break;
                }
                case 6:{
                    word = "盘块96~~~111";
                    break;
                }
                case 7:{
                    word = "盘块112~~~127";
                    break;
                }
                case 8:{
                    word = "盘块128~~~143";
                    break;
                }
                case 9:{
                    word = "盘块144~~~159";
                    break;
                }
                case 10:{
                    word = "盘块160~~~175";
                    break;
                }
                case 11:{
                    word = "盘块176~~~191";
                    break;
                }
                case 12:{
                    word = "盘块192~~~207";
                    break;
                }
                case 13:{
                    word = "盘块208~~~223";
                    break;
                }
                case 14:{
                    word = "盘块224~~~239";
                    break;
                }
                case 15:{
                    word = "盘块240~~~255";
                    break;
                }
            }
            tips[i] = new Label(word);
            gridPane.add(tips[i],0,i);

        }
        Label[][] labels = new Label[16][16];
        Disk[] disks = FAT.getFAT().getDisk();
        if(disks.length > 0){
            for(int i = 0;i < 16;i++){
                for(int j = 0;j < 16;j++){
                    if(disks[i * 16 + j].isFreeBlock()){
                        labels[i][j] = new Label();
                        labels[i][j].setPrefSize(w,h);
                        labels[i][j].setStyle("-fx-background-color: #57af28");
                    }else {
                        labels[i][j] = new Label();
                        labels[i][j].setPrefSize(w,h);
                        labels[i][j].setStyle("-fx-background-color: #f35a28");
                    }
                    gridPane.add(labels[i][j],j+1,i);
                }

            }
        }
        //gridPane.setPadding(new Insets(5));
        gridPane.setHgap(5);//水平间隔
        gridPane.setVgap(5);//纵向间隔
        initThePieChart();
        initTheTableView();
    }

    private void initThePieChart(){
        /**
         * 以下为设置饼图
         * */
        pieChart.setData(getChartData());
        //设置饼图颜色
        p1.getNode().setStyle("-fx-background-color: #f35a28");
        p2.getNode().setStyle("-fx-background-color: #57af28");
        //设置饼图数据下标颜色
        pieChart.setStyle("CHART_COLOR_1:#f35a28;CHART_COLOR_2:#57af28;");
        //设置饼图的标题
        pieChart.setTitle("磁盘分配情况");
    }

    private ObservableList<PieChart.Data> getChartData() {
        ObservableList<PieChart.Data> answer = FXCollections.observableArrayList();
        int free = 0;
        for(Disk d : FAT.getFAT().getDisk()){
            if(d.isFreeBlock()){
                free++;
            }
        }
        p1 = new PieChart.Data("已分配盘块", 256-free);
        p2 = new PieChart.Data("未分配盘块", free);
        answer.addAll(p1, p2);
        return answer;
    }

    private void initTheTableView(){
        //初始化表格
        diskDataTableView = new TableView<>();
        diskDataTableView.getItems().clear();
        diskDataTableView.refresh();
        //初始化表格数据
        diskData.clear();
        //设置表格列
        TableColumn diskNumCol = new TableColumn("盘块号");
        TableColumn diskIndexCol = new TableColumn("索引");
        TableColumn fileNameCol = new TableColumn("文档名称");
        TableColumn typeCol = new TableColumn("文档类型");
        TableColumn fileLengthCol = new TableColumn("文档长度");
        TableColumn absoluteAddressCol = new TableColumn("文档地址");
        //列属性绑定
        diskNumCol.setCellValueFactory(new PropertyValueFactory<>("diskNum"));
        diskIndexCol.setCellValueFactory(new PropertyValueFactory<>("diskIndex"));
        fileNameCol.setCellValueFactory(new PropertyValueFactory<>("fileName"));
        typeCol.setCellValueFactory(new PropertyValueFactory<>("type"));
        fileLengthCol.setCellValueFactory(new PropertyValueFactory<>("fileLength"));
        absoluteAddressCol.setCellValueFactory(new PropertyValueFactory<>("absoluteAddress"));
        //添加进入表格
        diskDataTableView.getColumns().addAll(diskNumCol,diskIndexCol,fileNameCol,typeCol,fileLengthCol,absoluteAddressCol);
        diskDataTableView.setEditable(true);
        diskDataTableView.setItems(diskData);
        int i = 0;
        for(Disk d : FAT.getFAT().getDisk()){
            if(!d.isFreeBlock()){
                String name = new String("");
                String path = new String("");
                String length;
                if(d.getObject() instanceof simulateOS.entity.File){
                    name = ((simulateOS.entity.File) d.getObject()).getFileName();
                    path = ((simulateOS.entity.File) d.getObject()).getLocation();
                    length = String.valueOf(((simulateOS.entity.File) d.getObject()).getLength());
                }else{
                    name = ((Folder) d.getObject()).getFolderName();
                    path = ((Folder) d.getObject()).getLocation();
                    length = String.valueOf(1);
                }
                diskData.add(new DiskData("盘块号:"+i,String.valueOf(d.getIndex()),name,d.getType(),length,path));
            }
            i++;
        }
    }
}
